package com.ruoyi.contract.tools;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileUtil {

    // 创建指定名称的文件夹
    public static void createFolder(String folderPath) {
        File folder = new File(folderPath);
        // 检查文件夹是否存在
        if (folder.exists()) {
            System.out.println("文件夹已存在: " + folderPath);
            return; // 文件夹已存在，不执行创建
        }

        // 创建新文件夹
        if (folder.mkdirs()) {
            System.out.println("文件夹创建成功: " + folderPath);
        } else {
            System.out.println("文件夹创建失败: " + folderPath);
        }
    }

    // 在指定文件夹中创建子文件夹
    public static String createSubFolder(String parentFolderPath, String subFolderName) {
        String subFolderPath = parentFolderPath + File.separator + subFolderName;
        createFolder(subFolderPath);
        return subFolderPath;
    }

    // 将文件放入指定文件夹
    public static void copyFileToFolder(File sourceFile, String destinationFolderPath) throws IOException {
        File destinationFile = new File(destinationFolderPath, sourceFile.getName());
        try (InputStream in = new FileInputStream(sourceFile);
             OutputStream out = new FileOutputStream(destinationFile)) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            System.out.println("文件已复制到指定文件夹: " + destinationFile.getAbsolutePath());
        }
    }

    // 删除文件夹及其内容
    public static void deleteDirectory(Path directory) throws IOException {
        Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file); // 删除文件
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir); // 删除目录
                return FileVisitResult.CONTINUE;
            }
        });
    }

    // 压缩文件夹并返回压缩文件路径
    public static String zipFolder(Path sourceDir, String zipFilePath) throws IOException {
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
            compressDirectory(sourceDir.toFile(), sourceDir.getFileName().toString(), zos);
        }
        System.out.println("文件夹已压缩: " + zipFilePath);
        return zipFilePath; // 返回压缩文件路径
    }

    private static void compressDirectory(File folder, String basePath, ZipOutputStream zos) throws IOException {
        for (File file : folder.listFiles()) {
            String entryName = basePath + "/" + file.getName();
            if (file.isDirectory()) {
                compressDirectory(file, entryName, zos);
            } else {
                try (FileInputStream fis = new FileInputStream(file)) {
                    zos.putNextEntry(new ZipEntry(entryName));
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = fis.read(buffer)) > 0) {
                        zos.write(buffer, 0, len);
                    }
                    zos.closeEntry();
                }
            }
        }
    }

    // 示例：主方法
    public static void main(String[] args) {
        String parentFolderPath = "/path/to/parentFolder"; // 替换为实际路径
        createFolder(parentFolderPath);

        // 创建子文件夹
        createSubFolder(parentFolderPath, "subFolder");

        // 创建子子文件夹（示例）
        createSubFolder(parentFolderPath + "/subFolder", "subSubFolder");

        // 复制文件到指定文件夹
        try {
            File sourceFile = new File("/path/to/source/file.txt"); // 替换为实际文件路径
            copyFileToFolder(sourceFile, parentFolderPath + "/subFolder");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 压缩文件夹并返回压缩文件路径
        String zipFilePath = parentFolderPath + "/subFolder.zip"; // 压缩文件的路径
        try {
            String returnedZipPath = zipFolder(Paths.get(parentFolderPath + "/subFolder"), zipFilePath);
            System.out.println("返回的压缩文件路径: " + returnedZipPath);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 删除文件夹及其内容
        try {
            deleteDirectory(Paths.get(parentFolderPath));
            System.out.println("已删除文件夹及其内容: " + parentFolderPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

